"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lei.chang on '23/04/2023'
    comment: agent相关
"""
from sqlalchemy import func
from sqlalchemy.dialects.mysql import VARCHAR, INTEGER, DATETIME, TIMESTAMP, BOOLEAN, FLOAT

from project.core.base import CustomModel, CustomList
from project.core.base import Column


class Agent(CustomModel):
    """ 智能体
    """
    __tablename__ = "agent"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    code = Column(VARCHAR(30), nullable=False, server_default='', comment='智能体编号')
    title = Column(VARCHAR(30), nullable=False, server_default='', comment='标题')
    image = Column(VARCHAR(200), nullable=False, server_default='', comment='知识库图片')
    prologue = Column(VARCHAR(200), nullable=False, server_default='', comment='开场白')
    ai_config = Column(VARCHAR(200), nullable=False, server_default='', comment='AI配置')
    operator_code = Column(VARCHAR(30), nullable=False, server_default='', comment='所有者')
    organization_code = Column(VARCHAR(20), nullable=False, server_default='', comment='机构编号', is_displayed=False)
    knowledge_ids = Column(CustomList, nullable=False, server_default='', comment='关联知识库id列表')
    bind_knowledge_count = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='关联知识库数量')

    is_default = Column(BOOLEAN, nullable=False, server_default='0', comment='是否默认 0: 非默认 1: 默认')

    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class AgentKnowledgeRelation(CustomModel):
    """ 智能体和知识库关联表
    """
    __tablename__ = "agent_knowledge_relation"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    agent_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='智能体id')
    knowledge_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='知识库id')
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class Chat(CustomModel):
    """ agent对话表
    """
    __tablename__ = "agent_chat"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    conversation_id = Column(VARCHAR(100), nullable=False, server_default='', comment='会话id')
    agent_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='所关联的数字人ID')
    title = Column(VARCHAR(30), nullable=False, server_default='', comment='标题')
    message_count = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='消息总数')
    remark = Column(VARCHAR(200), nullable=False, server_default='', comment='备注信息')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now())
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class ChatMessage(CustomModel):
    """ 对话消息
    """
    __tablename__ = "agent_chat_message"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    chat_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='agent对话id')
    role = Column(VARCHAR(30), nullable=False, server_default='', comment='角色 user(用户)/agent(智能体)')
    message = Column(VARCHAR(1000), nullable=False, server_default='', comment='对话内容')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now())
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)
